package com.lvyq.myplayer.ui.fragment

import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.lvyq.myplayer.R
import com.lvyq.myplayer.base.BaseFragment
import com.lvyq.myplayer.database.PlayerDatabase
import com.lvyq.myplayer.model.cache.PlayCacheViewModel
import com.lvyq.myplayer.model.cache.PlayCacheViewModelFactory
import com.lvyq.myplayer.model.history.PlayHistoryViewModel
import com.lvyq.myplayer.model.history.PlayHistoryViewModelFactory
import com.lvyq.myplayer.ui.activity.cache.IndexCacheSongActivity
import com.lvyq.myplayer.ui.activity.collect.IndexCollectActivity
import com.lvyq.myplayer.ui.activity.history.PlayHistoryActivity
import com.lvyq.myplayer.util.ToolBarManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 文件描述：UserCenterFragment 个人中心
 * 作者：lvyq
 * 创建时间：2025-06-17
 * 修改时间：2025-06-17
 */
class UserCenterFragment:ToolBarManager, BaseFragment(), View.OnClickListener {

    override lateinit var toolbar: Toolbar
    lateinit var  myCollectionView:LinearLayout
    lateinit var playHistoryLayoutView:LinearLayout
    lateinit var playCacheCountView:LinearLayout
    lateinit var playHistoryCount:TextView
    lateinit var playCacheCount:TextView

    private val playHistoryViewModel: PlayHistoryViewModel by viewModels {
        PlayHistoryViewModelFactory(PlayerDatabase.getDatabase(requireContext()).playHistoryDao())
    }

    private val playCacheViewModel:PlayCacheViewModel by viewModels {
        PlayCacheViewModelFactory(PlayerDatabase.getDatabase(requireContext()).playCacheDao())
    }



    override fun initView(): View? {
        var view = View.inflate(context, R.layout.fragment_user_center, null)

        myCollectionView=view.findViewById(R.id.myCollectionLayout)
        playHistoryLayoutView=view.findViewById(R.id.playHistoryLayout)
        playCacheCountView =view.findViewById(R.id.playCacheCountLayout)
        playHistoryCount=view.findViewById(R.id.playHistoryCount)
        playCacheCount=view.findViewById(R.id.playCacheCount)
        toolbar=view.findViewById(R.id.toolBar)

        initSettingToolBar("个人中心")

        return view
    }

    override fun initData() {
        super.initData()

        lifecycleScope.launch(Dispatchers.Main) {
            var count = playHistoryViewModel.count()
            count.collect{
                playHistoryCount.text = it.toString()
            }
        }

        //TODO 缓存单曲.暂时只存储获取单曲类，预留电台类型
        lifecycleScope.launch {
            var cacheCount = playCacheViewModel.count(1)
            cacheCount.collect{
                playCacheCount.text = it.toString()
            }
        }
    }

    override fun initListener() {
        super.initListener()
        myCollectionView.setOnClickListener(this)
        playHistoryLayoutView.setOnClickListener(this)
        playCacheCountView.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.myCollectionLayout->{
                var intent = Intent(context, IndexCollectActivity::class.java)
                //跳转每日推荐
                context?.let {
                    it.startActivity(intent)
                }
            }
            //历史记录
            R.id.playHistoryLayout->{
                var intent = Intent(context, PlayHistoryActivity::class.java)
                context?.let {
                    it.startActivity(intent)
                }
            }

            //缓存单曲
            R.id.playCacheCountLayout->{
                var intent= Intent(context,IndexCacheSongActivity::class.java)
                context?.let {
                    it.startActivity(intent)
                }
            }
            }
        }
}